#ifndef KLCUSTOMDELEGATE_H
#define KLCUSTOMDELEGATE_H

#include <QStyledItemDelegate>
#include <QIcon>

// 定义按钮状态枚举
enum ButtonState {
    StateNormal,
    StateSelected,
    StateDisabled
};


class KLCustomDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit KLCustomDelegate(QObject* parent = nullptr);
    ~KLCustomDelegate() override;
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
signals:
    void dataChamged();

public slots:
    void saveNewDataTobase(const QString &name, const QString &mcc, const QString &mnc, const bool isNewItem);

private:
    void handleButtonClick(const QModelIndex &index, QAbstractItemModel *model);

    void showComboBox(const QModelIndex &index, QAbstractItemModel *model);

private:
    mutable QMap<QModelIndex, ButtonState> m_buttonStates;

    QIcon *m_pOpenIcon;
    QIcon *m_pCloseIcon;


    //EditOperator editOperatorWindow;
    QAbstractItemModel *modelEdited;
    QModelIndex editedModelIndex;

    ButtonState getState(const QModelIndex &index) const;
    void toggleState(const QModelIndex &index);
    void update(const QModelIndex &index) const;
};

#endif // KLCUSTOMDELEGATE_H
